﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;
namespace Hr.Controllers
{
    public class clientController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly Ifirst_kindServices fdServices;
        private readonly Isecond_kindServices skServices;
        private readonly Ithird_kindServices tkServices;
        private readonly MyDbContext myDbContext;

        public clientController(MyDbContext myDbContext,ILogger<clientController>logger, Ifirst_kindServices fdServices, Isecond_kindServices skServices,Ithird_kindServices tkServices)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.fdServices = fdServices;
            this.skServices = skServices;
            this.tkServices = tkServices;
        }

      
        //I级机构
        [HttpPost]
        public async Task<IActionResult> first_kinds()
        {
            //需求：获取数据，显示在页面
            List<config_file_first_kind> dt = await fdServices.selectAll();
            // ViewBag.s = dt;
            //ViewData.Model = dt;
            return Ok(dt);
        }

        [HttpGet]
        public IActionResult first_kind()
        {
            return View();
        }

        //增加I级
        [HttpPost]
        public async Task<ActionResult> first_kind_register([FromForm] Econfig_file_first_kind test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.config_file_first_kind.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }

        [HttpGet]
        public ActionResult first_kind_register()
        {
            return View();
        }

        [HttpGet]
        public ActionResult first_kind_register_success()
        {
            return View();
        }

        [HttpGet]
        public ActionResult first_kind_change_success()
        {
            return View();
        }

        [HttpGet]
        public ActionResult first_kind_change(int id)
        {
            ViewData["id"] = id;
            //var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return View();
        }

        //查找，修改I级
        [HttpPost]
        public async Task<ActionResult> first_kind_change1(int id)
        {
            var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }

        [HttpPut]
        public async Task<IActionResult> first_kind_change([FromForm] Econfig_file_first_kind test)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDbContext.config_file_first_kind.Attach(test);
                myDbContext.Entry(test).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDbContext.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());


        }

        //删除I级
        [HttpDelete]
        public async Task<int> first_kind_Delete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_file_first_kind test = await myDbContext.config_file_first_kind.FindAsync(id);
                myDbContext.config_file_first_kind.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }


        //II级

        [HttpPost]
        public async Task<IActionResult> second_kinds()
        {
            //需求：获取数据，显示在页面
            List<config_file_second_kind> dt = await skServices.selectAll();
            // ViewBag.s = dt;
            //ViewData.Model = dt;
            return Ok(dt);
        }

        [HttpGet]
        public IActionResult second_kind()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> GetAllFId()
        {
            List<config_file_first_kind> list = await fdServices.selectAll();
            return Content(JsonConvert.SerializeObject(list));
        }

        //增加II级
        [HttpPost]
        public async Task<ActionResult> second_kind_register([FromForm] Econfig_file_second_kind test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.config_file_second_kind.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }

        [HttpGet]
        public ActionResult second_kind_register()
        {
            return View();
        }

        [HttpGet]
        public ActionResult second_kind_register_success()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> GetAllFSId()
        {
            List<config_file_first_kind> first = await fdServices.selectAll();
            List<config_file_second_kind> second = await skServices.selectAll();
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["first"] = first;
            di["second"] = second;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPost]
        //三级根据选择一级查询二级
        public async Task<ActionResult> GetfirstId(string id)
        {
            List<config_file_second_kind> second = await skServices.GetFirstId(id);
            return Content(JsonConvert.SerializeObject(second));
        }


        [HttpGet]
        public ActionResult second_kind_change_success()
        {
            return View();
        }

        //II级
        [HttpGet]
        public ActionResult second_kind_change(int id)
        {
            ViewData["id"] = id;
            //var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return View();
        }

        //查找，修改II级
        [HttpPost]
        public async Task<ActionResult> second_kind_change1(int id)
        {
            var fd = await myDbContext.config_file_second_kind.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }

        [HttpPut]
        public async Task<IActionResult> second_kind_change([FromForm] Econfig_file_second_kind test)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDbContext.config_file_second_kind.Attach(test);
                myDbContext.Entry(test).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDbContext.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());

        }

        //删除II级
        [HttpDelete]
        public async Task<int> second_kind_Delete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_file_second_kind test = await myDbContext.config_file_second_kind.FindAsync(id);
                myDbContext.config_file_second_kind.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }



        //III级
        [HttpPost]
        public async Task<IActionResult> third_kinds()
        {
            //需求：获取数据，显示在页面
            List<config_file_third_kind> dt = await tkServices.selectAll();
            // ViewBag.s = dt;
            //ViewData.Model = dt;
            return Ok(dt);
        }

        [HttpGet]
        public IActionResult third_kind()
        {
            return View();
        }

        [HttpGet]
        public ActionResult third_kind_register_success()
        {
            return View();
        }

        [HttpGet]
        public ActionResult third_kind_change_success()
        {
            return View();
        }

        //修改III级
        [HttpGet]
        public ActionResult third_kind_change(int id)
        {
            ViewData["id"] = id;
            //var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> third_kind_change1(int id)
        {
            var fd = await myDbContext.config_file_third_kind.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }

        [HttpPut]
        public async Task<IActionResult> third_kind_change([FromForm] Econfig_file_third_kind test)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDbContext.config_file_third_kind.Attach(test);
                myDbContext.Entry(test).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDbContext.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());

        }

        //增加III级
        [HttpPost]
        public async Task<ActionResult> third_kind_register([FromForm] Econfig_file_third_kind test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.config_file_third_kind.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }

        [HttpGet]
        public ActionResult third_kind_register()
        {
            return View();
        }

        //删除III级
        [HttpDelete]
        public async Task<int> third_kind_Delete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_file_third_kind test = await myDbContext.config_file_third_kind.FindAsync(id);
                myDbContext.config_file_third_kind.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }


    }
}
